package com.example.zzl.adapter.SearchFragment;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.example.zzl.R;
import com.example.zzl.custom.widget.MyGridView;
import com.example.zzl.model.bean.SearchFragment.SearchBrand;
import com.example.zzl.utils.ListViewUtils;
import com.example.zzl.view.activity.SearchActivity.SearchResultActivity;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created by zhang on 2017/2/9.
 */

public class ListViewBrandAdapter extends BaseAdapter implements AdapterView.OnItemClickListener{

    private Context context;
    private ArrayList<SearchBrand.ResultBeanX> list;
    private ArrayList<SearchBrand.ResultBeanX.ResultBean> resultBeen;
    private CifGvAdapter adapter;
//    private ArrayList<SearchBrand.ResultBeanX> resultBeanXes;
//    private ArrayList<SearchBrand.ResultBeanX.ResultBean> resultBeen;

    public ListViewBrandAdapter(Context context, ArrayList<SearchBrand.ResultBeanX> list) {
        this.context = context;
        this.list = list;
        resultBeen = new ArrayList<>();
//        resultBeanXes = new ArrayList<>();
//        resultBeen = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

//    @Override
//    public int getItemViewType(int position) {
//        Object o = list.get(position);
//        Log.e("data",o.toString());
//        if(o instanceof SearchBrand.ResultBeanX){
//
//            resultBeanXes.add((SearchBrand.ResultBeanX) list.get(position));
//            //Log.e("data",resultBeanXes.size()+"--->");
//            return  0;
//        }else if(o instanceof SearchBrand.ResultBeanX.ResultBean){
//            resultBeen.add((SearchBrand.ResultBeanX.ResultBean) list.get(position));
//            return 1;
//        }
//        return super.getItemViewType(position);
//    }
//
//    @Override
//    public int getViewTypeCount() {
//        return 2;
//    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder1 vh1;
        //Log.e("data",list.get(i).toString());
        //ViewHolder2 vh2;
//        switch(getItemViewType(i)){
//            case 0:
        if (view == null) {
            view = View.inflate(context, R.layout.brand_listview_item_1, null);
            vh1 = new ViewHolder1();
            vh1.tv_1 = (TextView) view.findViewById(R.id.tv_brand_item_1);
            vh1.tv_2 = (TextView) view.findViewById(R.id.tv_brand_item_2);
            vh1.gv = (MyGridView) view.findViewById(R.id.gv_brand_item_2);
            viewGroup.setTag(vh1);
        } else {
            vh1 = (ViewHolder1) view.getTag();
        }
        if (vh1 != null) {
            //Log.e("data",list.size()+">>>>>>");
            vh1.tv_1.setText(list.get(i).getCatname());
            vh1.tv_2.setText("热销品牌");
            //Log.e("data",list.size()+"------");
            resultBeen = (ArrayList<SearchBrand.ResultBeanX.ResultBean>) list.get(i).getResult();
            final ArrayList<Object> objects = new ArrayList<>();
            for (int i1 = 0; i1 < resultBeen.size(); i1++) {
                objects.add(resultBeen.get(i1));
                //Log.e("data", Arrays.toString(list.get(i).getResult().toArray()));
            }
            adapter = new CifGvAdapter(context, objects);
            vh1.gv.setAdapter(adapter);
            final ArrayList<SearchBrand.ResultBeanX.ResultBean> results = new ArrayList<>();
            for (int i1 = 0; i1 < objects.size(); i1++) {
                results.add((SearchBrand.ResultBeanX.ResultBean) objects.get(i1));
            }
            vh1.gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Intent intent = new Intent(context, SearchResultActivity.class);
                    if(results.get(i).getStrBrandName().equals(results.get(i).getStrBrandEngName())) {
                        intent.putExtra("search", results.get(i).getStrBrandEngName());
                    }else{
                        intent.putExtra("search", results.get(i).getStrBrandEngName() + " " + results.get(i).getStrBrandName());
                    }
                    intent.putExtra("brandurl",results.get(i).getStrBrandUrl());
                    intent.putExtra("type","Cate");
                    context.startActivity(intent);

                }
            });
            ListViewUtils.calGridViewWidthAndHeigh1(4, vh1.gv);
        }
        //break;
//            case 1:
//                if(view == null){
//                    view = View.inflate(context,R.layout.brand_listview_item_2,null);
//                    vh2 = new ViewHolder2();
//                    vh2.gv = (MyGridView) view.findViewById(R.id.gv_brand_item_2);
//                    view.setTag(vh2);
//                }else{
//                    vh2 = (ViewHolder2) view.getTag();
//                }
//                ArrayList<Object> objects = new ArrayList<>();
//                for (int i1 = 0; i1 < resultBeen.size(); i1++) {
//                    objects.add(resultBeen.get(i1));
//                }
//                CifGvAdapter adapter = new CifGvAdapter(context,objects);
//                vh2.gv.setAdapter(adapter);
//                break;
        //}
        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }

    class ViewHolder1 {
        TextView tv_1, tv_2;
        MyGridView gv;
    }


}
